package com.shycloud.mido.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.common.minio.service.MinioTemplate;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.music.domain.MisMusicError;
import com.shycloud.mido.music.dto.MusicErrorSearchDTO;
import com.shycloud.mido.music.mapper.MisMusicErrorMapper;
import com.shycloud.mido.music.service.MisMusicErrorService;
import com.shycloud.mido.music.vo.MisMusicVO;
import com.shycloud.mido.music.vo.MusicErrorVO;

import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author nianhua.jiang
 * @description 曲目错误模块controller
 * @date 2020/5/11
 */
@RestController
@ApiOperation("曲目错误模块controller")
@RequestMapping("/musicError")
@AllArgsConstructor
@ApiIgnore
public class MusicErrorController {

	private MinioTemplate minioTemplate;
	/** 曲目错误模块 service */
	private MisMusicErrorService musicErrorService;

	private MisMusicErrorMapper errorMapper;

	/**
	 * 提交错误信息
	 *
	 * @param musicError
	 * @return com.shycloud.mido.common.core.util.R
	 * @author nianhua.jiang
	 * @date 2020/5/11 17:22
	 */
	@ApiOperation("提交错误信息")
	@PostMapping("/addMusicError")
	public R addMusicError(@RequestBody MisMusicError musicError) {

		if (StringUtils.isNotBlank(musicError.getErrorRemarks())) {
			if (musicError.getErrorRemarks().length() > 400) {
				return new R(CommonConstants.FAIL, "超出字符限制", false);
			}
		}

		//用户ID
		musicError.setUserId(SecurityUtils.getMember().getId());
		//创建时间
		musicError.setCreateTime(new Date());
		return new R(musicErrorService.save(musicError));
	}

	/**
	 * 分页获取曲目错误信息
	 *
	 * @return com.shycloud.mido.common.core.util.R
	 * @param9
	 * @author nianhua.jiang
	 * @date 2020/5/11 17:34
	 */
	@GetMapping("/getMusicError")
	@ApiOperation("App分页获取曲目报错")
	public R getMusicError(Integer current, Integer size) {
		Page page = new Page(current, size);
		//member_id
		String memberId = SecurityUtils.getMember().getId();
		return new R(musicErrorService.getMusicError(page, memberId));
	}


	@GetMapping("/getMusicErrorPage")
	@ApiOperation("后台获取曲目报错页面")
	public R getMusicErrorPage(MusicErrorSearchDTO search) {
		IPage<MusicErrorVO> page = errorMapper.page(search);
		return new R(page);
	}

	@PostMapping("/updateMusicError")
	@ApiOperation("更新错误信息")
	public R updateMusicError(@RequestBody MisMusicError error) {
		return new R(musicErrorService.updateById(error));
	}

	@PostMapping("/uploadImg")
	@ApiOperation("上传曲谱报错图片")
	public R uploadImg(@RequestParam("img") MultipartFile img) {

		//提取后缀
		//提取图片名称
		String fileName = img.getOriginalFilename().replace(" ", "");
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);

		String uuid = UUID.randomUUID().toString();
		//提取图片名称
		String imgName = "error-" + uuid.replace("-", "") + "." + suffix;
		//初始化map结果集
		Map<String, String> resultMap = new HashMap<>(4);
		//名称
		resultMap.put("name", imgName);
		try {
			//上传操作
			resultMap.put("url", minioTemplate.uploadFile(img.getInputStream(), imgName));
		} catch (Exception e) {
			return R.builder().code(CommonConstants.FAIL).msg(e.getLocalizedMessage()).build();
		}
		return R.builder().data(resultMap).build();

	}

}
